﻿using Hotel.Model.Member;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.DAL
{
    public class MemberDAL
    {
        MyDBContext db;

        public MemberDAL(MyDBContext db)
        {
            this.db = db;
        }


        /// <summary>
        /// 完成会员注册
        /// </summary>
        /// <param name="info">注册的实体</param>
        /// <returns>返回受影响的行数</returns>
        public int PostMemberRegiter(T_Member info)
        {
            try
            {
                db.T_Member.Add(info);
                return db.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 查询一手机号是否存在
        /// </summary>
        /// <param name="phone">手机号</param>
        /// <returns>返回符合条件的数据</returns>
        public int SearchPhone(string phone)
        {
            try
            {
                return db.T_Member.Where(c => c.M_Phone == phone).Count();
            }
            catch (Exception)
            {

                throw;
            }
        }
       
        /// <summary>
        /// 会员登录
        /// </summary>
        /// <param name="phone">手机号</param>
        /// <param name="pwd">密码</param>
        /// <returns>返回符合条件的数据</returns>
        public T_Member GetMemeberLogin(string phone,string pwd) {
            try
            {
                T_Member info = db.T_Member.Where(c=>c.M_Phone == phone && c.M_Pwd == pwd).FirstOrDefault();
                return info;
            }
            catch (Exception)
            {

                throw;
            }
        
        }
        /// <summary>
        /// 获取会员列表
        /// </summary>
        /// <param name="phone">根据手机号查询</param>
        /// <returns>返回符合条件的数据</returns>
        public List<T_Member> GetMembers(string? phone="")
        {
            try
            {
                List<T_Member> list = db.T_Member.Where(c=>c.IsDelete == false).ToList();

                if (!string.IsNullOrEmpty(phone))
                {
                    list = list.Where(c=>c.M_Phone == phone).ToList();
                }

                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        public int GetDelMemberItem(int mid)
        {
            try
            {
                var info = db.T_Member.Find(mid);
                info.IsDelete = true;
                return db.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据id修改状态
        /// </summary>
        /// <param name="mid">id</param>
        /// <param name="state">转台</param>
        /// <returns></returns>
        public int GetUpdateMemberItem(int mid,int state) {
            try
            {
                var info = db.T_Member.Find(mid);
                info.M_State = state;
                return db.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="infos"></param>
        /// <returns></returns>
        public int PostAddMemberList(List<T_Member> infos)
        {
            try
            {
                db.T_Member.AddRange(infos);
                return db.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据会员的ID获取会员信息
        /// </summary>
        /// <param name="Mid">会员id</param>
        /// <returns>返回符合条件的数据</returns>
        public T_Member GetMember(int Mid) {

            try
            {
                T_Member info = db.T_Member.Find(Mid);
                return info;
            }
            catch (Exception)
            {

                throw;
            }

        }
        /// <summary>
        /// 对会员进行修改
        /// </summary>
        /// <param name="Mid">会员id</param>
        /// <returns>返回影响的行数</returns>
        public int PostUpdateMember(T_Member info)
        {
            try
            {
                //1.根据id找到当前条数据
                var oldInfo = db.T_Member.Find(info.M_Id);
                //2.完成修改操作
                    oldInfo.M_Number = info.M_Number;
                    oldInfo.M_Money = info.M_Money;
                return db.SaveChanges();    

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 增加会员等级
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int PostAddMemberLevel(T_Member_Level info) {
            try
            {
                //状态默认开启
                info.LevelState = 1;
                //修改时间默认创建时间
                info.LevelChangeDate = DateTime.Now;
                //增加
                db.T_Member_Level.Add(info);
                return db.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }

        }
        /// <summary>
        /// 查等级名称是否存在
        /// </summary>
        /// <param name="levelName">等级名称</param>
        /// <returns>符合条件的数据</returns>

        public int SearchLevelName(string levelName)
        {
            try
            {
                int res = db.T_Member_Level.Where(c=>c.LevelName == levelName).Count();
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }



       /// <summary>
       /// 等级列表
       /// </summary>
       /// <param name="totalPage">总页数</param>
       /// <param name="totalCount">总条数</param>
       /// <param name="LevelName">等级名称</param>
       /// <param name="pageIndex">页码</param>
       /// <param name="pageSize">页容量</param>
       /// <returns></returns>
        public List<T_Member_Level> GetMemberLevelList(out int totalPage,out int totalCount,string? LevelName="",int pageIndex=1,int pageSize=2)
        {
            try
            {
                //所有的等级列表
                List<T_Member_Level>  list = db.T_Member_Level.Where(c=>c.IsDelete == false).ToList();

                //根据等级名称模糊查询
                if(!string.IsNullOrEmpty(LevelName))
                {
                    list = list.Where(c=>c.LevelName.Contains(LevelName)).ToList();
                }

                //分页
                //总条数
                totalCount  = list.Count();
                //总页数   共13  每页显示3  总共分5页
                totalPage = (int)Math.Ceiling(totalCount * 1.0 / pageSize);

                //分页
                list = list.OrderBy(c=>c.LevelId).Skip((pageIndex-1)*pageSize).Take(pageSize).ToList();

                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 增加会员权益
        /// </summary>
        /// <param name="info">会员权益</param>
        /// <returns>返回影响的行数</returns>
        public int PostAddMemberRight(T_Member_Right info)
        {
            try
            {
                db.T_Member_Right.Add(info);
                return db.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据权益名称查询
        /// </summary>
        /// <param name="rightName"></param>
        /// <returns></returns>
        public int SeachRightName(string rightName)
        {
            try
            {
                int res = db.T_Member_Right.Where(c=>c.RightName == rightName).Count();
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取权益列表
        /// </summary>
        /// <returns></returns>
        public List<T_Member_Right> GetMemberRights()
        {
            try
            {
                List<T_Member_Right> list = db.T_Member_Right.ToList();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 增加等级权益
        /// </summary>
        /// <param name="rightArr">权益id</param>
        /// <param name="levelId">等级id</param>
        /// <returns></returns>
        public int PostAddLevelRight(string rightArr,int levelId)
        {
            try
            {
                //思路：先删原来所有的权益，增加新的权益
                //1.先根据等级id，获取他所有的权益列表
                //查出的对应等级的权益
                var list = db.T_Level_Right.Where(c=>c.LevelId == levelId).ToList();
                //2.在数据表中删除他所有的权益  
                foreach (var item in list)
                {
                    db.T_Level_Right.Remove(item);
                }
                //3.从新增加新的权益
               //讲字符串转成数组  1 2 3
                string[]  arr =  rightArr.Split(',');

                foreach(var item in arr)
                {
                   var t =  new T_Level_Right{
                        LevelId = levelId,
                        RightId =Convert.ToInt32(item)   
                    };
                    
                    db.T_Level_Right.Add(t);
                }

                return db.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据等级id去查他的权益
        /// </summary>
        /// <param name="levevlId">等级id</param>
        /// <returns>返回符合条件的数据</returns>

        public List<T_Level_Right> GetLevelRightList(int levevlId)
        {
            try
            {
                List<T_Level_Right> list = db.T_Level_Right.Where(c => c.LevelId == levevlId).ToList();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }


    }

    
}

